package com.smartplatform.websocket;

import com.smartplatform.event.DeviceColorCommandEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author 咕唧
 * @Date 2022/6/4 20:04
 * @Version 1.0
 */
public class MyRGBColorHandler extends MyBaseWebSocketHandler {
    private static Map<String, Long> heartbeatTime = new ConcurrentHashMap<>();
    private static Map<String, WebSocketSession> allClient = new ConcurrentHashMap<>();

    @Async
    @EventListener
    public void onEvent(DeviceColorCommandEvent event) {
        //检查心跳
        System.err.println("收到了发送命令的事件"+event);
        WebSocketSession session = getAllClient().get(event.getDeviceId());
        if (session != null && session.isOpen()) {
            try {
                session.sendMessage(new TextMessage(event.getCommand()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public boolean isNeedUpdateOnline() {
        return false;
    }

    @Override
    protected Map<String, Long> getHeartbeatTime() {
        return heartbeatTime;
    }

    @Override
    protected Map<String, WebSocketSession> getAllClient() {
        return allClient;
    }
}
